import requests
import csv

f = open('data1.csv', mode='w', encoding='utf-8', newline='')
csv_writer = csv.DictWriter(f, fieldnames=[
    # '排行',
    '电影名字',
    '上映天数',
    '影片类型',
    '总场次',
    '上午场次',
    '下午场次',
    '上映日期'
])
csv_writer.writeheader()
def func(page, ):
    url = 'https://ys.endata.cn/enlib-api/api/rowpiece/getRowPiece_MovieByShowCount_List.do'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'
    }
    data = {
        'r': '0.8620868798373524',
        'rowpiecetype': '1',
        'datetype': 'Day',
        'date': '2022-11-27',
        'sdate': '2022-11-27',
        'edate': '2022-11-27',
        'pageindex': f' {page}',
        'pagesize': ' 10',
    }
    resp = requests.post(url=url, headers=headers, data=data)
    json_data = resp.json()

    table = json_data['data']['table0']
    for item in table:
        Irank = item['Irank']  # 排行
        MovieName = item['MovieName']  # 电影名字
        ReleaseDay = item['ReleaseDay']  # 上映天数
        GenreMain = item['GenreMain']  # 影片类型
        ShowCount = item['ShowCount']  # 总场次
        SwShowCount = item['SwShowCount']  # 上午场次
        XwShowCount = item['XwShowCount']  # 下午场次
        ReleaseDate = item['ReleaseDate']   # 上映日期
        dic = {
            # '排行': Irank,
            '电影名字': MovieName,
            '上映天数': ReleaseDay,
            '影片类型': GenreMain,
            '总场次': ShowCount,
            '上午场次': SwShowCount,
            '下午场次': XwShowCount,
            '上映日期': ReleaseDate
        }
        csv_writer.writerow(dic)
        print(MovieName, ReleaseDay, GenreMain, ShowCount, SwShowCount, XwShowCount, ReleaseDate)


if __name__ == '__main__':
    for page in range(1, 10):
        func(page)